package com.theobserver.theobserver;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.RequestPasswordResetCallback;


public class UserPasswdModifyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_passwd_modify);



        final String Email = AVUser.getCurrentUser().getEmail();//当前用户邮箱

        Button bt=(Button)findViewById(R.id.button_confirm);
        bt.setOnClickListener(new View.OnClickListener() {
                                  @Override
                                  public void onClick(View view) {
                                      final EditText userpwd = (EditText) findViewById(R.id.userpwd);
                                      final String pwd = userpwd.getText().toString();//输入的框中的密码

                                      AVUser.logInInBackground(Email, pwd, new LogInCallback<AVUser>() {
                                          @Override
                                          public void done(AVUser avUser, AVException e) {
                                              if (e == null) {

                                                  AVUser.requestPasswordResetInBackground(Email, new RequestPasswordResetCallback() {
                                                      @Override
                                                      public void done(AVException e) {
                                                          if (e == null) {
                                                              Toast.makeText(UserPasswdModifyActivity.this, "请前往邮箱修改密码！", Toast.LENGTH_SHORT).show();
                                                              AVUser.getCurrentUser().logOut();
                                                              finish();

                                                          } else {

                                                              Toast.makeText(UserPasswdModifyActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                                                          }
                                                      }
                                                  });
                                              }else
                                                  Toast.makeText(UserPasswdModifyActivity.this, "原密码错误，请重新输入！", Toast.LENGTH_SHORT).show();
                                          }
                                      });
                                  }
                              });







        //对返回按钮设置监听
        TextView tv=(TextView)findViewById(R.id.textView_return);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserPasswdModifyActivity.this.finish();
            }
        });

    }
}
